package com.jeecg.demo.api;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeecg.demo.entity.AppCustomServiceGuideEntity;
import com.jeecg.demo.entity.AppOnlineServerDetailEntity;
import com.jeecg.demo.entity.AppOnlineServerEntity;
import com.jeecg.demo.entity.AppServiceBespeakEntity;
import com.jeecg.demo.entity.AppServiceHomeEntity;
import com.jeecg.demo.entity.ServiceGuide;
import com.jeecg.demo.entity.SysDictEntity;
import com.jeecg.demo.entity.Sys_dict;
import com.jeecg.demo.service.AppOnlineServerDetailServiceI;
import com.jeecg.demo.service.AppOnlineServerServiceI;
import com.jeecg.demo.service.AppServiceBespeakServiceI;
import com.jeecg.demo.service.SysDictServiceI;
import com.jeecg.demo.util.TimerUtil;

@Api(value = "AppServiceBespeak", description = "预约表", tags = "AppServiceBespeak")
@Controller
@RequestMapping("/service_bespeak")
public class Service_bespeak {
	@Autowired
	private AppServiceBespeakServiceI appServiceBespeakServiceI;
	@Autowired
	private SystemService systemService;
	@Autowired
	private SysDictServiceI sysDictServiceI;
	@Autowired
	private AppOnlineServerDetailServiceI appOnlineServerDetailServiceI;
	@Autowired
	private AppOnlineServerServiceI appOnlineServerServiceI;

	/**
	 * 获取客服首页电话号码
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getCSTel", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "预约信息", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> Tellist() {
		List<Map<String, Object>> listMap = systemService.getList(AppServiceHomeEntity.class);
		return Result.success(listMap);
	}

	/**
	 * 预约信息列表
	 * 
	 * @param userid
	 * @param pageSize
	 * @param curPage
	 * @return
	 */
	@RequestMapping(value = "/bespeakList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "预约信息", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> list(
			@RequestParam(required = false) BigDecimal userid // 用户编号
	) {
		// 设置查询条件
		if (StringUtil.isEmptyDig(userid)) {
			return null;
		}
		String sql = "SELECT t.SERVICE_BESPEAK_ID,t.APP_USER_ID, t.MANAGER_USER_ID, t.SERVICE_BESPEAK_TYPE_ID ,"
				+ "s.DICNAME,t.BESPEAK_CONTENT, to_char(t.BESPEAK_DATE,'yyyy-mm-dd') as BESPEAK_DATE, t.RESPONSE,a.dicname as state,"
				+ "t.qqcode,t.telphone FROM app_service_bespeak t,sys_dict s,sys_dict a  where t.state=a.diccode "
				+ "and a.dictype='BESPEAKSTATUS' and t.SERVICE_BESPEAK_TYPE_ID=s.DICCODE and s.DICTYPE =  'BESPEAK'   "
				+ "and  t.app_user_id =?  order by SERVICE_BESPEAK_ID desc";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql,
				userid);
		return Result.success(listMap);
	}

	/**
	 * 马上预约
	 * 
	 * @param userid
	 * @param qq
	 * @param phone
	 * @param typeid
	 * @param marker
	 * @return
	 */
	@RequestMapping(value = "/addBespeak", method = RequestMethod.POST)
	@ResponseBody
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 5)
	@ApiOperation(value = "添加预约信息")
	public ResponseMessage<?> create(
			@RequestParam(required = false) BigDecimal userid, // 用户编号
			@RequestParam(required = false) String qq, // QQ
			@RequestParam(required = false) String phone, // 电话
			@RequestParam(required = false) String typeid,// 问题类型
			@RequestParam(required = false) String marker // 备注
	) {
		if (StringUtil.isEmptyDig(userid)) {
			return Result.error("用户编号不能为空");
		}
		if (StringUtil.isEmpty(qq) && StringUtil.isEmpty(phone)) {
			return Result.error("QQ和电话至少写一个");
		}
		if (StringUtil.isEmpty(typeid)) {
			return Result.error("请选择问题类型");
		}
		if (StringUtil.isEmpty(marker)) {
			return Result.error("请输入预约内容");
		}
		AppServiceBespeakEntity appServiceBespeakEntity = new AppServiceBespeakEntity();
		try {
			appServiceBespeakEntity.setAppUserId(userid);
			appServiceBespeakEntity.setBespeakDate(TimerUtil
					.getCurrentTimeStamp());
			appServiceBespeakEntity.setQqcode(qq);
			appServiceBespeakEntity.setTelphone(phone);
			appServiceBespeakEntity.setServiceBespeakTypeId(typeid);
			appServiceBespeakEntity.setState("1");
			appServiceBespeakEntity.setBespeakContent(marker);
			appServiceBespeakServiceI.save(appServiceBespeakEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("预约失败");
		}
		return Result.success(appServiceBespeakEntity);
	}

	/**
	 * 获取客服服务类型信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/bespeakType", method = RequestMethod.POST)
	@ResponseBody
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 5)
	@ApiOperation(value = "客服服务类型")
	public ResponseMessage<List<Map<String, Object>>> list() {
		List<Map<String, Object>> listMap = systemService.getList(SysDictEntity.class);
		return Result.success(listMap);
	}

	/**
	 * 取服务指南大类
	 * 
	 * @return
	 */
	@RequestMapping(value = "/cataList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "服务指南大类", produces = "application/json")
	public ResponseMessage<Map<String, Object>> CataList() {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String hql1 = "FROM SysDictEntity "
					+ " WHERE ISVALID =  '1' AND DICTYPE =  'SERVICEGUIDE' ORDER BY ORDERID ASC";
			List<SysDictEntity> SysDictlist = systemService.findHql(hql1);
			List<Sys_dict> list = new ArrayList<Sys_dict>();
			for (SysDictEntity sys_Dict : SysDictlist) {
				Sys_dict s = new Sys_dict();
				s.setDICCODE(sys_Dict.getDiccode());
				s.setDICNAME(sys_Dict.getDicname());
				//取服务指南子类
				String hql = "FROM AppCustomServiceGuideEntity WHERE ISVAILD =  '1' AND CUSTOM_SERVICE_TYPE_ID=?"
						+ " ORDER BY ordernum asc,CREATE_DATE desc";
				List<AppCustomServiceGuideEntity> ServiceGuideList = systemService
						.findHql(hql, sys_Dict.getDiccode());
				s.setList(ServiceGuideList);
				list.add(s);
			}
            if (list.size() > 0) {
                map.put("data", list);
            }
            else {
            	map.put("data", "");
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  Result.success(map);
	}
	/**
	 * 取服务子类
	 */
	@RequestMapping(value = "/cataDetails", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "服务指南详情", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> Detaillist(
			@RequestParam(required = false) String id) {
		if (StringUtil.isEmpty(id)) {
			return null;
		}
		String sql = "select t.* from APP_CUSTOM_SERVICE_GUIDE t where T.CUSTOM_SERVICE_GUIDE_ID = ?";
		List<Map<String, Object>> listMap = systemService
				.findForJdbc(sql, id);
		return Result.success(listMap);
	}
	/**
	 * 获取物流信息链接及相关信息
	 * 
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/getInfoListByWuliu", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "预约信息", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> Wuliulist(
			@RequestParam(required = false) String type) {
		if (StringUtil.isEmpty(type)) {
			return null;
		}
		String sql = "select t.* from APP_WEB_LINK t where t.flage=?";
		List<Map<String, Object>> listMap = systemService
				.findForJdbc(sql, type);
		return Result.success(listMap);
	}

	/**
	 * 在线业务咨询列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/CustomAskList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "在线业务咨询列表", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> CustomAsklist(
			@RequestParam(required = false, defaultValue = "10") int pageSize, // 页数
			@RequestParam(required = false, defaultValue = "1") int page // 页数
		) {
		String sql = "SELECT a.SERVER_ONLINE_ID,to_char(a.ASK_DATE, 'yyyy-mm-dd hh24:mi:ss') as ASK_DATE ,"
				+ " a.THEME, a.APP_USER_ID  FROM app_online_server  a  WHERE  a.ISVALID =  '1' order by a.SERVER_ONLINE_ID desc";
		List<Map<String, Object>> listMap = systemService.findForJdbcParam(sql,page, pageSize);
		return Result.success(listMap);
	}

	/**
	 * 保存 在线咨询 发起的主题信息
	 * 
	 * @param userid
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "/addAakCustom", method = RequestMethod.POST)
	@ResponseBody
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 5)
	@ApiOperation(value = "保存客服在线咨询 发起的主题")
	public ResponseMessage<?> create(
			@RequestParam(required = false) int userid, // 用户编号
			@RequestParam(required = false) String content // 咨询问题
	) {
		if (StringUtil.isEmpty(userid + "")) {
			return Result.error("用户编号不能为空");
		}
		if (StringUtil.isEmpty(content)) {
			return Result.error("请输入内容");
		}
		AppOnlineServerEntity appOnlineServer = new AppOnlineServerEntity();
		try {
			appOnlineServer.setAppUserId(userid);
			appOnlineServer.setTheme(content);
			appOnlineServer.setAskDate(TimerUtil.getCurrentTimeStamp());
			appOnlineServer.setIsvalid("0");// 审核：0未审核
			appOnlineServerServiceI.save(appOnlineServer);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("咨询失败");
		}

		return Result.success(appOnlineServer);
	}

	/**
	 * 客服在线咨询明细信息
	 * 
	 * @param questid
	 * @return
	 */
	@RequestMapping(value = "/CustomAnswerList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "在线咨询明细信息")
	public ResponseMessage<List<Map<String, Object>>> Customjson(
			@RequestParam(required = false) int questid // 问题编号
	) {
		String sql = "SELECT a.APP_ONLINE_SERVER_DETAIL, a.SERVER_ONLINE_ID, a.SPEAK_ID, a.ONLINE_RESULT,"
				+ "to_char(a.SPEAK_DATE, 'yyyy-mm-dd hh24:mi:ss') as SPEAK_DATE,a.source  FROM "
				+ " app_online_server_detail  a WHERE a.ISVALID =  '1' and a.SERVER_ONLINE_ID=? "
				+ " order by a.APP_ONLINE_SERVER_DETAIL desc";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql,
				questid);
		return Result.success(listMap);
	}

	/**
	 * 客服在线咨询明细信息保存
	 * 
	 * @param userid
	 * @param questid
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "/addAnswerCustom", method = RequestMethod.POST)
	@ResponseBody
	@Transactional(isolation = Isolation.READ_COMMITTED, timeout = 5)
	@ApiOperation(value = "在线咨询明细信息保存")
	public ResponseMessage<?> addAnswerCustom(
			@RequestParam(required = false) int userid, // 用户编号
			@RequestParam(required = false) int questid, // 问题编号
			@RequestParam(required = false) String content, // 回复
			@RequestParam(required = false) String loginname) {
		if (StringUtil.isEmpty(userid + "")) {
			return Result.error("用户编号不能为空");
		}
		if (StringUtil.isEmpty(questid + "")) {
			return Result.error("问题编号不能为空");
		}
		if (StringUtil.isEmpty(content)) {
			return Result.error("请输入回复内容");
		}
		if (StringUtil.isEmpty(loginname)) {
			return Result.error("没有获取用户名");
		}
		AppOnlineServerDetailEntity appOnlineServerDetailEntity = new AppOnlineServerDetailEntity();
		try {
			appOnlineServerDetailEntity.setServerOnlineId(questid);
			appOnlineServerDetailEntity.setSpeakId(userid);
			appOnlineServerDetailEntity.setOnlineResult(content);
			appOnlineServerDetailEntity.setSpeakDate(TimerUtil
					.getCurrentTimeStamp());
			appOnlineServerDetailEntity.setIsvalid("0");
			int a = 0;
			if (loginname == "管理员") {
				a = 0;
			} else {
				a = 1;
			}
			appOnlineServerDetailEntity.setSource(a);
			appOnlineServerDetailServiceI.save(appOnlineServerDetailEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("回复失败");
		}

		return Result.success(appOnlineServerDetailEntity);
	}
}
